<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class muser extends MY_Model {

    var $theme_data = array(
        'layout_main' => 'main/user/layout',
    );
    public $mytable = "user";
    public $mytable_privilege = "privileges";
    public $primaryKey = "use_id";
    public $resCode = "USER";
    public $use_id;
    public $use_name;
    public $use_account;
    public $use_avatar;
    public $fb_id;
	
	public $regex_email = '/^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/';
	public $regex_username = '/^[a-zA-Z0-9_.+-]{0,32}$/';

    function __construct() {
        parent::__construct();
        $this->setInfo();
    }

    function field() {
        $fieldData = array();
		$fieldData['use_account'] = '';
		$fieldData['use_name'] = '';
		$fieldData['use_password'] = '';
		$fieldData['use_email'] = '';
		$fieldData['use_gender'] = '';
		$fieldData['use_avatar'] = '';
		$fieldData['use_status'] = 1;
		$fieldData['use_job'] = '';
		$fieldData['use_phone'] = '';
		$fieldData['use_address'] = '';
		$fieldData['use_scores'] = 0;
		$fieldData['use_rank'] = 0;
		$fieldData['use_preference'] = 0;
		$fieldData['use_city'] = 0;
		$fieldData['facebook_id'] = null;
		$fieldData['facebook_data'] = null;
		return $fieldData;
    }
	
    function field_privilege() {
        $fieldData = array();
        $fieldData['use_id'] = 0;
        $fieldData['pri_level'] = 0;
        $fieldData['res_id'] = 0;
        return $fieldData;
    }
	
    function registry($data) {
		
		if ($this->get($data["use_account"], 'use_account')) {
			return -1;
		}
		// $data["use_email"];
		// $data["use_account"] = 
		if(!isset($data["use_avatar"]) || $data["use_avatar"] == '') {
			$data["use_avatar"] = 'dataupload/user/avatar/default_avatar.jpg';
		}
		$data["use_status"] = 1;
		$data["use_create"] = time();
		$data["use_passtimeupdate"] = time();
		$salt = $this->random_salt();
		$data["use_password"] = $this->hash_pass($data["use_password"], $salt) . ":" . $salt;
        
        $this->db->insert($this->mytable, $data);
        return $this->db->insert_id();
    }

    /**
     * Hash password
     * @return String 
     */
    public function hash_pass($pass, $salt = '') {
        return md5(base64_encode($pass.$salt));
    }

    /**
     * Random Salt
     * @return String(10)
     */
    public function random_salt() {
        $d = base64_encode(md5(time() . rand()));
        return substr($d, 0, 10);
    }

    public function random_key($num_char = 20) {
        $act = base64_encode(md5(time() . rand()));
        return substr($act, 0, $num_char);
    }

    function get($val = '', $param = 'use_id') {
        $this->db->from($this->mytable);
        $this->db->where($param, $val);
        return $this->db->get()->first_row();
    }

    function insert($arr_insert) {
        $arr_insert = $this->msystem->insert_security($arr_insert, $this->field());
        $this->db->insert($this->mytable, $arr_insert);
        return $this->db->insert_id();
    }

    function update($val = '', $param = null, $arr_update) {
        if ($param == null)
            $param = $this->primaryKey;

        if ($val != '')
            $this->db->where($param, $val);
        $arr_update = $this->msystem->update_security($arr_update, $this->field());
        $this->db->update($this->mytable, $arr_update);
    }

	public function getLogin() {
		$user =  $this->session->all_userdata();
		return $user;
	}
    function setLogin($user) {
        if (!$user) {
            return false;
        }
		$user = (array) $user;
		$user['isLogin'] = 1;
		unset($user['use_password']);
        
        $this->session->set_userdata($user);
        return true;
    }

    public function checkLogin() {
        return $this->session->userdata('isLogin');
    }

    function update_password($use_id = 0, $user_password = '') {
        $salt = $this->random_salt();
        $new_passwd = $this->hash_pass($user_password, $salt);
        $arr_update = array();
        $arr_update['use_password'] = $new_passwd . ":" . $salt;
        $arr_update['use_passtimeupdate'] = time();
        $this->db->where('use_id', $use_id);
        $this->db->update($this->mytable, $arr_update);
    }

    function set_activation($use_id = 0) {
        $arr_update = array();
        $arr_update['use_status'] = 1;
        $arr_update['use_passtimeupdate'] = time();
        $this->db->where('use_id', $use_id);
        $this->db->update($this->mytable, $arr_update);
    }

    function getAll($param = '', $val = '', $start = 0, $limit = 10) {
        $this->db->from($this->mytable);
        if ($param != '' && $val != '')
            $this->db->where($param, $val);
        $this->db->limit($limit, $start);
        return $this->db->get()->result();
    }

    function gets($val = '', $param = null) {
        if ($param == null)
            $param = 'use_id';

        $this->db->from($this->mytable);
        if ($val != '')
            $this->db->where($param, $val);
       // $this->db->order_by("use_id DESC");
        return $this->db->get()->result();
    }

    function logout() {
        $this->session->sess_destroy();
        return 1;
    }

    function info($use_id) {
        
    }

    function setInfo() {
        if ($this->checkLogin()) {
            $this->use_id = $this->session->userdata('use_id');
            $this->use_account = $this->session->userdata('use_account');
            $this->use_name = $this->session->userdata('use_name');
            $this->use_avatar = $this->session->userdata('use_avatar');
        }
		
    }

    function getsUserGroup($condition = array()) {
        $this->db->from("user_group");
        $this->db->where($condition);
        return $this->db->get()->result();
    }
	
    function updatePermission($val = '', $param = null, $arr_update) {
	
		if ($param == null)
            $param = $this->primaryKey;
        if (is_array($val) && $val != array())
            $this->db->where($val);
        else if ($val != '')
            $this->db->where($param, $val);
		$arr_update = $this->msystem->update_security($arr_update, $this->field_privilege());
        $this->db->update($this->mytable_privilege, $arr_update);
    }
	
	function insertPermission($arr_insert) {
        $arr_insert = $this->msystem->insert_security($arr_insert, $this->field_privilege());
        $this->db->insert($this->mytable_privilege, $arr_insert);
        return $this->db->insert_id();
    }
	
    function deletePermission($val = '', $param = null) {
        if ($val != '') {
            if ($param == null)
                $param = $this->primaryKey;

            $this->db->where($param, $val);
            $this->db->delete("project_privilege");
        }
    }
	
	function parse_email($email =''){
		if($email !='')
		{
			return array_shift(explode("@",$email));
		}
		return null;
		
	}
	function download_avatar($url, $user_name = ''){
		//check allow_url_fopen
		
		$this->load->library("get_data_html");
		$this->load->model("sserver");
		$data = array();
		$data['url'] = $url;
		$data['path'] = './dataupload/user/avatar/'.$user_name.'.jpg';
		$data['iscache'] = '1';
		$result = $this->get_data_html->get_data_post('http://'.$this->sserver->main_server_public.'/server/mainserver_downloadfile',$data);
		
		/*
		$this->load->model("simage");
		$patch = 'dataupload/user/avatar/'.$user_name.'.jpg';
		$this->simage->create_dirs($patch);
		$content = file_get_contents($url);
		file_put_contents('./'.$patch, $content);
		$this->simage->cache($patch);
		*/
		return 'dataupload/user/avatar/'.$user_name.'.jpg';
		
	}
}

?>